package application;

import java.lang.Character;
import java.lang.Integer;
import java.util.HashMap;
import java.util.Map;

public class pString {

    String pfx;
    Map<Character, Integer> priority;

    @Override
    public String toString() {
        return "postfixString{" + "pfx=" + pfx + '}';
    }

    public pString(String postfix) {
        this.pfx = postfix;
        priority = new HashMap<Character, Integer>();
        priority.put('(', 0);
        priority.put(')', 1);
        priority.put('+', 2);
        priority.put('-', 2);
        priority.put('*', 3);
        priority.put('/', 3);
    }

    public String stringToPostfix() {
        String result = "";
        strStack s = new strStack();
        for (int i = 0; i < pfx.length(); i++) {
            char c = pfx.charAt(i);
            if (c != '(' && c != ')' && c != '+' && c != '-' && c != '*' && c != '/') {
                result += c;
            } else if ((c == '+' || c == '-') && s.isEmpty()) {
                s.push(c == '+' ? '+' : '-');
            } else if ((c == '+' || c == '-') && !s.isEmpty()) {
                while (!s.isEmpty() || (s.getLast().equals('+') || s.getLast().equals('-'))) {
                    result += s.pop();
                }
                s.push(c == '+' ? '+' : '-');
            }
        }
        if (!s.isEmpty()) {
            result += s.pop();
        }
        s.showStack();
        return result;
    }
}
